package cn.kailangtu.myapplication.data;

import android.app.Activity;
import android.util.Log;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.List;

import cn.kailangtu.myapplication.data.specialview.BaseSpecialView;
import cn.kailangtu.myapplication.enums.ActionCode;
import cn.kailangtu.myapplication.tools.FomaterUtils;
import cn.kailangtu.myapplication.tools.MediaPlayerUtils;
import cn.kailangtu.myapplication.view.AutoMeterView;
import cn.kailangtu.myapplication.view.LevelImageView;
import cn.kailangtu.serialport.CommonUtils;

public class CanDataItem {
    private int lastValue;
    private int address;
    private String title;
    private int value;
    private String unit;
    private int dataIndex;
    private int byteCount;
    private Integer bitIndex;
    private Float jingdu;
    private float jinduValue;
    private Float pianyi;
    private Integer view;
    private Integer highPosition;
    private Integer minValue;
    private int keyboardNum = -1;
    private ActionCode actionCode = ActionCode.设置值;
    private List<String> ls_文字集合 = null;
    private List<Integer> ls_图片集合 = null;
    private List<Integer> ls_颜色集合 = null;
    private List<Integer> ls_显示 = null;
    private List<MatchItem> ls_值匹配 = null;
    private List<CustomItem> ls_自定义 = null;
    private List<Integer> 对比值 = null;
    private List<SoundItem> ls_声音 = null;
    private BaseSpecialView specialView;
    /***
     * 刷新模式：0为轮询；1为值变触发
     */
    private int refreshMode = 0;


    public CanDataItem()
    {
        unit="";
    }

    public void refresh()
    {
        byte[] source =  CanDataManager.getInstance().getData(address);
        Log.d("SHOOTLEEITEMREFRESH",CommonUtils.ByteArrayToHexString(source));
        if(byteCount == 1)
        {
            this.value = source[dataIndex] & 0xFF;
        }
        else if(byteCount == 2)
        {
            if( highPosition != null && highPosition == 1)
            {
                this.value = CommonUtils.byteArrayToInt16(new byte[]{source[dataIndex + 1], source[dataIndex]});

            }
            else {
                this.value = CommonUtils.byteArrayToInt16(new byte[]{source[dataIndex], source[dataIndex+1]});
            }
        }
        else if(byteCount == 3)
        {
            if( highPosition != null && highPosition == 1)
            {
                this.value = CommonUtils.combineBytesToInt(source[dataIndex + 1], source[dataIndex]);
            }
            else {
                this.value = CommonUtils.combineBytesToInt(source[dataIndex], source[dataIndex+1]);

            }
        }
        else if(byteCount == 4)
        {
            if( highPosition != null && highPosition == 1)
            {
                this.value = CommonUtils.byteArrayToInt(new byte[]{source[dataIndex + 3], source[dataIndex + 2], source[dataIndex + 1], source[dataIndex]});
            }
            else {

                this.value = CommonUtils.byteArrayToInt(new byte[]{source[dataIndex], source[dataIndex + 1], source[dataIndex + 2], source[dataIndex+3]});

            }
        }
        if(pianyi != null && pianyi != 0)
        {
            this.value = this.value+ pianyi.intValue();
        }

        if(minValue != null)
        {
            if(this.value < minValue)
            {
                this.value = minValue;
            }
        }

        if(jingdu != null && jingdu != 0)
        {
            jinduValue = this.value * jingdu;
            if(minValue != null && jinduValue < minValue)
            {
                jinduValue = minValue;
            }
        }
        if(bitIndex != null &&  bitIndex > -1)
        {
            this.value = (this.value >> bitIndex) & 1;
        }



    }

    private int getValue(int address,int dataIndex,int dataCount,Integer bitIndex)
    {
        byte[] source =  CanDataManager.getInstance().getData(address);
        int result = 0;
        Log.d("SHOOTLEEITEMREFRESHGETVALUE",CommonUtils.ByteArrayToHexString(source));
        if(dataCount == 1)
        {
            result = source[dataIndex] & 0xFF;
        }
        else if(dataCount == 2)
        {
            if( highPosition != null && highPosition == 1)
            {
                result = CommonUtils.combineBytesToInt(source[dataIndex + 1], source[dataIndex]);

            }
            else {
                result = CommonUtils.combineBytesToInt(source[dataIndex], source[dataIndex+1]);
            }
        }
        else if(dataCount == 3)
        {
            if( highPosition != null && highPosition == 1)
            {
                result = CommonUtils.combineBytesToInt(source[dataIndex + 1], source[dataIndex]);
            }
            else {
                result = CommonUtils.combineBytesToInt(source[dataIndex], source[dataIndex+1]);

            }
        }
        else if(dataCount == 4)
        {
            if( highPosition != null && highPosition == 1)
            {
                result = CommonUtils.byteArrayToInt(new byte[]{source[dataIndex + 3], source[dataIndex + 2], source[dataIndex + 1], source[dataIndex]});

            }
            else {
                result = CommonUtils.byteArrayToInt(new byte[]{source[dataIndex], source[dataIndex + 1], source[dataIndex + 2], source[dataIndex+3]});
            }
        }
        if(bitIndex != null &&  bitIndex > -1)
        {
            result = (result >> bitIndex) & 1;
        }

        if(minValue != null)
        {
            if(result < minValue)
            {
                result = minValue;
            }
        }

        Log.d("SHOOTLEEITEMREFRESHGETVALUE","RESULT:"+result);
        return result;
    }


    public String getValueString()
    {
        if(ls_文字集合 != null)
        {
            if(ls_文字集合.size() > value)
            {
                return ls_文字集合.get(value)+unit;
            }
        }
        if(jingdu != null &&  jingdu != 0)
        {
            jinduValue = this.value * jingdu;
            return FomaterUtils.format2f( jingdu,this.jinduValue , this.unit);
        }
        return this.value+this.unit;
    }

    public Float getFloatValue()
    {
        refresh();
        if(jingdu != null && jingdu != 0)
        {
            return new Float(this.value * jingdu);
        }
        return new Float(value);
    }

    public void resetView(Activity context)
    {
        if(refreshMode == 0) {

            refresh();
            if(specialView != null)
            {
                specialView.setNewValue(value);
            }

            if (view == null)
                return;
            View currentView = context.findViewById(view);
            if( currentView instanceof AutoMeterView)
            {
                resetAutoMeterView((AutoMeterView) currentView);
            }
            else if (currentView instanceof LevelImageView) {
                resetLevelImageView((LevelImageView) currentView);
            } else {
                if (actionCode == null) {
                    actionCode = ActionCode.设置值;
                }
                switch (actionCode) {
                    case 改图片:
                        setSourceImage((ImageView) currentView);
                        break;
                    case 改字色:
                        setFontColor((TextView) currentView);
                        break;
                    case 改底图:
                        setBgImage(currentView);
                        break;
                    case 改底色:
                        setBgColor(currentView);
                        break;
                    case 改显示:
                        setVisibility(currentView);
                        break;
                    case 设置值:
                        setText((TextView) currentView);
                        break;
                    case 值匹配:
                        setMatch((TextView) currentView);
                        break;
                    case 自定义:
                        setCustom((TextView) currentView);
                        break;
                }
            }
        }
    }


    private void resetAutoMeterView(AutoMeterView autoMeterView)
    {
        if(jinduValue != 0)
        {
            autoMeterView.setDegreeOffset(jinduValue);
        }
        else {
            autoMeterView.setDegreeOffset(value);
        }
    }

    private void resetLevelImageView(LevelImageView levelImageView)
    {

        if(value == 1)
        {
            //1级，致命
            if(this.ls_图片集合 != null && this.ls_图片集合.size() > 1) {
                levelImageView.setImageResource(ls_图片集合.get(1));
            }
        }
        else
        {
            //不是1级，查看是不是2级
            CanID canIdTwo = CanID.build(levelImageView.getLevelTwo());
            int levelTwoValue = getValue(canIdTwo.get地址(), canIdTwo.get数据索引下标(), canIdTwo.get数据长度(), canIdTwo.get比特位());
            if(levelTwoValue == 1)
            {
                //2级，严重
                if(this.ls_图片集合 != null && this.ls_图片集合.size() > 2) {
                    levelImageView.setImageResource(ls_图片集合.get(2));
                }
            }
            else
            {
                //不是1级2级，查看是不是3级
                CanID canIdThree = CanID.build(levelImageView.getLevelThree());
                int levelThreeValue = getValue(canIdThree.get地址(), canIdThree.get数据索引下标(), canIdThree.get数据长度(), canIdThree.get比特位());
                if(levelThreeValue == 1)
                {
                    //3级，一般
                    if(this.ls_图片集合 != null && this.ls_图片集合.size() > 3) {
                        levelImageView.setImageResource(ls_图片集合.get(3));
                    }
                }
                else
                {
                    //不是1，2，3级，使用默认图片
                    if(this.ls_图片集合 != null && this.ls_图片集合.size() > 0) {
                        levelImageView.setImageResource(ls_图片集合.get(0));
                    }
                }
            }
        }
    }


    private void setMatch(TextView currentView) {
        MatchItem matchItem = null;
        MatchItem defaultItem = null;


        if (this.ls_值匹配 != null && this.ls_值匹配.size() > 0) {

            if(对比值 != null && 对比值.size()>0)
            {
                for(int currentValue : 对比值) {
                    for (MatchItem currSi : ls_值匹配) {
                        if (!currSi.is默认() && value == currentValue && currSi.get对比值() == currentValue) {
                            matchItem = currSi;
                            break;
                        }
                        if (currSi.is默认()) {
                            defaultItem = currSi;
                        }
                    }
                }
            }
        }
        if(matchItem == null && defaultItem != null)
        {
            matchItem = defaultItem;
        }



        if (matchItem != null) {
            if (matchItem.get字体颜色() != null) {
                currentView.setTextColor(matchItem.get字体颜色());
            }
            if (matchItem.get字号() != null) {
                currentView.setTextSize(matchItem.get字号());
            }
            if (matchItem.get文本() != null) {
                currentView.setText(matchItem.get文本());
            }
        }
    }

    private void setSound(View currentView)
    {
        SoundItem soundItem = null;
        if(this.ls_声音 != null && this.ls_声音.size() > value)
        {
            soundItem = this.ls_声音.get(value);
        }
        if(soundItem != null){
            if(soundItem.getVisibleValue() != null)
            {
                currentView.setVisibility(soundItem.getVisibleValue().intValue());
            }
            if(isValueChanged())
            {
                MediaPlayerUtils.getInstance().playResource(soundItem.getResourceId());
            }
        }
    }

    private void setCustom(TextView currentView) {

        CustomItem customItem = null;
        if (this.ls_自定义 != null && this.ls_自定义.size() > value) {
            customItem = this.ls_自定义.get(value);
        }


        if (customItem != null) {
            if (customItem.get字体颜色() != null) {
                currentView.setTextColor(customItem.get字体颜色());
            }
            if (customItem.get字号() != null) {

                currentView.setTextSize(TypedValue.COMPLEX_UNIT_SP,customItem.get字号());
                currentView.setIncludeFontPadding(false);
                currentView.setGravity(Gravity.CENTER);
                currentView.requestLayout();
            }
            if (customItem.get文本() != null) {
                currentView.setText(customItem.get文本());
            }
        }
    }

    private void setText(TextView currentView)
    {
        currentView.setText(getValueString());
    }

    private void setBgImage(View currentView)
    {
        if(this.ls_图片集合 != null && this.ls_图片集合.size() > value) {
            currentView.setBackgroundResource(ls_图片集合.get(value));
        }
    }

    private void setSourceImage(ImageView currentView){
        if(this.ls_图片集合 != null && this.ls_图片集合.size() > value) {
            currentView.setImageResource(ls_图片集合.get(value));
        }
    }

    private void setVisibility(View currentView)
    {
        if(this.ls_显示 != null && this.ls_显示.size()>  value)
        {
            currentView.setVisibility(ls_显示.get(value));
        }
    }

    private void setBgColor(View currentView)
    {
        if(this.ls_颜色集合.size() > value)
        {
            currentView.setBackgroundColor(ls_颜色集合.get(value));
        }
    }

    private void setFontColor(TextView currentView)
    {
        if(this.ls_颜色集合.size() > value)
        {
            currentView.setTextColor(ls_颜色集合.get(value));
        }
    }

    private void setShowOrHide(View currentView){
        if(this.ls_显示.size() > value)
        {
            currentView.setVisibility(ls_显示.get(value));
        }
    }



    @Override
    public String toString() {
        return this.title+":"+getValueString();
    }


    public int getAddress() {
        return address;
    }

    public void setAddress(int address) {
        this.address = address;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public int getValue() {
        return value;
    }

    public void setValue(int value) {
        this.value = value;
    }

    public String getUnit() {
        return unit;
    }

    public void setUnit(String unit) {
        this.unit = unit;
    }

    public int getDataIndex() {
        return dataIndex;
    }

    public void setDataIndex(int dataIndex) {
        this.dataIndex = dataIndex;
    }

    public int getByteCount() {
        return byteCount;
    }

    public void setByteCount(int byteCount) {
        this.byteCount = byteCount;
    }

    public Integer getBitIndex() {
        return bitIndex;
    }

    public void setBitIndex(Integer bitIndex) {
        this.bitIndex = bitIndex;
    }

    public Float getJingdu() {
        return jingdu;
    }

    public void setJingdu(Float jingdu) {
        this.jingdu = jingdu;
    }

    public Integer getHighPosition() {
        return highPosition;
    }

    public void setHighPosition(Integer highPosition) {
        this.highPosition = highPosition;
    }

    public float getJinduValue() {
        return jinduValue;
    }

    public void setJinduValue(float jinduValue) {
        this.jinduValue = jinduValue;
    }

    public Integer getView() {
        return view;
    }

    public void setView(Integer view) {
        this.view = view;
    }

    public int getKeyboardNum() {
        return keyboardNum;
    }

    public void setKeyboardNum(int keyboardNum) {
        this.keyboardNum = keyboardNum;
    }

    public Float getPianyi() {
        return pianyi;
    }

    public void setPianyi(Float pianyi) {
        this.pianyi = pianyi;
    }

    public ActionCode getActionCode() {
        return actionCode;
    }

    public void setActionCode(ActionCode actionCode) {
        this.actionCode = actionCode;
    }

    public List<String> getLs_文字集合() {
        return ls_文字集合;
    }

    public void setLs_文字集合(List<String> ls_文字集合) {
        this.ls_文字集合 = ls_文字集合;
    }

    public List<Integer> getLs_图片集合() {
        return ls_图片集合;
    }

    public void setLs_图片集合(List<Integer> ls_图片集合) {
        this.ls_图片集合 = ls_图片集合;
    }

    public List<Integer> getLs_颜色集合() {
        return ls_颜色集合;
    }

    public void setLs_颜色集合(List<Integer> ls_颜色集合) {
        this.ls_颜色集合 = ls_颜色集合;
    }

    public void setLs_显示(List<Integer> ls_显示){
        this.ls_显示 = ls_显示;
    }

    public int getRefreshMode() {
        return refreshMode;
    }

    public void setRefreshMode(int refreshMode) {
        this.refreshMode = refreshMode;
    }

    public List<Integer> getLs_显示() {
        return ls_显示;
    }

    public List<MatchItem> getLs_值匹配() {
        return ls_值匹配;
    }

    public void setLs_值匹配(List<MatchItem> ls_值匹配) {
        this.ls_值匹配 = ls_值匹配;
    }

    public List<CustomItem> getLs_自定义() {
        return ls_自定义;
    }

    public void setLs_自定义(List<CustomItem> ls_自定义) {
        this.ls_自定义 = ls_自定义;
    }

    public List<Integer> get对比值() {
        return 对比值;
    }

    public void set对比值(List<Integer> 对比值) {
        this.对比值 = 对比值;
    }

    public BaseSpecialView getSpecialView() {
        return specialView;
    }

    public void setSpecialView(BaseSpecialView specialView) {
        this.specialView = specialView;
    }

    public Integer getMinValue() {
        return minValue;
    }

    public void setMinValue(Integer minValue) {
        this.minValue = minValue;
    }

    public List<SoundItem> getLs_声音() {
        return ls_声音;
    }

    public void setLs_声音(List<SoundItem> ls_声音) {
        this.ls_声音 = ls_声音;
    }

    private boolean isValueChanged()
    {
        if(value != lastValue)
        {
            lastValue = value;
            return true;
        }

        return false;
    }
}
